package com.example.user.service;



import com.example.user.dao.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.PageParam;
import entity.Rsp;
import entity.Role;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public PageInfo<Role> searchSomeByPage(PageParam<Role> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Role> roleList=roleMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleList);

            return rolePageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Role> roleList=roleMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleList);

            return rolePageInfo;

        }

    }

    @Override
    public Role getOneById(String id) {
        return roleMapper.getOneById(id);
    }

    @Override
    public Rsp addOne(Role role) {

        role.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//        role.setCreateTime(new Date());



        return roleMapper.addOne(role)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp deleteOneTrue(String id) {



        return roleMapper.deleteOneTrue(id)==1 ? Rsp.success(null).msg("物理删除成功"):Rsp.fail().msg("物理删除失败");
    }

    @Override
    public Rsp update(Role role) {


        return roleMapper.update(role)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteLogical(Role role) {

        role.setIsDeleted(1);

        return roleMapper.update(role)==1?Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    @Override
    public Rsp recoverLogical(Role role) {

        role.setIsDeleted(0);

        return roleMapper.update(role)==1?Rsp.success(null).msg("逻辑恢复成功"):Rsp.fail().msg("逻辑恢复失败");
    }

    @Override
    public Rsp deleteBatchTrue(List<String> ids) {


        return roleMapper.deleteBatchTrue(ids)==ids.size()?Rsp.success(null).msg("批量物理删除成功"):Rsp.fail().msg("批量物理删除失败");
    }

    @Override
    public Rsp deleteBatchLogical(List<String> ids) {


        return roleMapper.deleteBatchLogical(ids)==ids.size()?Rsp.success(null).msg("批量物理删除成功"):Rsp.fail().msg("批量物理删除失败");
    }


    @Override
    public Rsp deleteSomeLogicalByCondition(Role role) {


        return roleMapper.deleteSomeLogicalByCondition(role)>0?Rsp.success(null).msg("条件逻辑物理删除成功"):Rsp.fail().msg("条件逻辑物理删除失败");
    }

    @Override
    public Rsp deleteSomeTrueByCondition(Role role) {


        return roleMapper.deleteSomeTrueByCondition(role)>0?Rsp.success(null).msg("条件逻辑物理删除成功"):Rsp.fail().msg("条件逻辑物理删除失败");
    }


}
